package org.atfeihu.springcloud.alibaba.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.atfeihu.springcloud.service.PaymentHystrixService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author wangfeihu
 * @create 2021/9/3 10:29
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "globalFallbackHandler")
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_ok(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_ok(id);
        log.info("==============>consumer_ret(paymentInfo_ok): " +result);
        return result;
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    /*@HystrixCommand(fallbackMethod = "paymentInfo_timeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })*/
    @HystrixCommand
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        int i = 10 / 0;
        String result = paymentHystrixService.paymentInfo_timeout(id);
        log.info("==============>consumer_ret(paymentInfo_timeout): " +result);
        return result;
    }

    // 兜底方法
    public String paymentInfo_timeoutHandler(@PathVariable("id") Integer id){
        return "线程池: " + Thread.currentThread().getName() + "; 系统(80)繁忙, 请稍后再试; id: " + id + "; o(╥﹏╥)o";
    }

    public String globalFallbackHandler() {
        return "consumer全局fallback";
    }
}
